using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;

using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ACM.cmsarea08.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DynUploader runat=server></{0}:DynUploadert>")]
    public class DynUploader : TextBox
    {
		private string uploadHandler = "/template/plugins/UploadObject/dynaupload.aspx";
    	
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string InstanceID
        {
            get
            {
                object o = ViewState["instance_id"];
                if (o == null) return string.Empty;
                else return o.ToString();
            }
            set
            {
                ViewState["instance_id"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string FileFormat
        {
            get
            {
                object o = ViewState["FileFormat"];
                if (o == null) return string.Empty;
                else return o.ToString();
            }
            set
            {
                ViewState["FileFormat"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Width
        {
            get
            {
                object o = ViewState["Width"];
                if (o == null) return "270";
                else return o.ToString();
            }
            set
            {
                ViewState["Width"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Height
        {
            get
            {
                object o = ViewState["Height"];
                if (o == null) return "75";
                else return o.ToString();
            }
            set
            {
                ViewState["Height"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string SavePath
        {
            get
            {
                object o = ViewState["SavePath"];
                if (o == null) return "/public/images/";
                else return o.ToString();
            }
            set
            {
                ViewState["SavePath"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CssClass
        {
            get
            {
                object o = ViewState["CssClass"];
                if (o == null) return string.Empty;
                else return o.ToString();
            }
            set
            {
                ViewState["CssClass"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ShowPreview
        {
            get
            {
                object o = ViewState["ShowPreview"];
                if (o == null) return "false";
                else return o.ToString();
            }
            set
            {
                ViewState["ShowPreview"] = value;
            }
        }

        public string PreviewWidth
        {
            get
            {
                object o = ViewState["PreviewWidth"];
                if (o == null) return "50";
                else return o.ToString();
            }
            set
            {
                ViewState["PreviewWidth"] = value;
            }
        }
        public string PreviewHeight
        {
            get
            {
                object o = ViewState["PreviewHeight"];
                if (o == null) return "50";
                else return o.ToString();
            }
            set
            {
                ViewState["PreviewHeight"] = value;
            }
        }
        public string MaxUploadSize
        {
            get
            {
                object o = ViewState["MaxUploadSize"];
                if (o == null) return "40960";
                else return o.ToString();
            }
            set
            {
                ViewState["MaxUploadSize"] = value;
            }
        }
        public bool UniqueName
        {
            get
            {
                object o = ViewState["UniqueName"];
                if (o == null) return false;
                else return (bool)o;
            }
            set
            {
                ViewState["UniqueName"] = value;
            }
        }
        
        public string Value {
        	get {
        		return this.Text;
        	}
        	set {
        		this.Text = value;
        	}
        }

		public DynUploader()
		{}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
		}
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
            string embedobjecturl = "/template/plugins/UploadObject/uploadobject.js";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.js");
            string cssembedobjecturl = "/template/plugins/UploadObject/uploadobject.css";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.css");
           // HttpContext.Current.Response.Write("--> " + embedobjecturl);
            if(this.Page is ACM.cmsarea08.Core.MainPage) {
//            	((ACM.cmsarea08.Core.MainPage)this.Page).RegisterMooTools();
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript("/template/plugins/mootools-1.2-core.js");
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript("/template/plugins/mootools-1.2-more.js");
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript(embedobjecturl);
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomCss(cssembedobjecturl);
            } else {
            	//includi lo script... mootools..
            	if(this.Page.Header!=null) {
            		System.Web.UI.HtmlControls.HtmlHead myhead = this.Page.Header;
					bool foundMo = false;
                    bool foundMoMore = false;
					bool foundEb = false;
                    bool foundEbc = false;
                    
					for(int i = 0; i <myhead.Controls.Count;i++) {
						if(myhead.Controls[i] is LiteralControl) {
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("mootools-1.2-core.js") != -1)
                            {
								foundMo=true;
							}
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("mootools-1.2-more.js") != -1)
                            {
                                foundMoMore = true;
                            }

							if(((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("uploadobject.js")!=-1) {
								foundEb=true;
							}
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("uploadobject.css") != -1)
                            {
                                foundEbc = true;
                            }

						}
					}
					if(!foundMo) {
                        LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\"/template/plugins/mootools-1.2-core.js\"></script>");
						myhead.Controls.Add(ct);			
					}
                    if (!foundMoMore)
                    {
                        LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\"/template/plugins/mootools-1.2-more.js\"></script>");
                        myhead.Controls.Add(ct);
                    }            		

					if(!foundEb) {
						LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\""+embedobjecturl+"\"></script>");
						myhead.Controls.Add(ct);			
					}
                    if (!foundEbc)
                    {
                        LiteralControl ct = new LiteralControl("<style type=\"text/css\" media=\"screen\">\n"+
                            "<!--\n"+
                            "@import url(\""+cssembedobjecturl+"\");\n"+
                            "-->\n</style>");
                        myhead.Controls.Add(ct);
                    }             		
            		
            	}
            }
        }		
        protected override void Render(HtmlTextWriter writer)
        {
			//this.ID
			//this.ClientID
			//this.UniqueID
			writer.Write("<div id=\""+this.ClientID+"cont\" class=\"dynuploadobject "+this.CssClass+"\" >");
			//scrivo il text box.
			writer.Write("<input type=\"text\" name=\"" +this.UniqueID +"\" id=\"" + this.ClientID + "\" Value=\"" + this.Text + "\" />");
		    writer.Write("</div>\n");

            string inst = this.InstanceID;
            if (inst == string.Empty) inst = "1";
		    writer.Write("<script language=\"javascript\" type=\"text/javascript\" defer=\"defer\">\n");
		    writer.Write("// <![CDATA[ \n");
		    writer.Write(" window." + this.ID.Trim() + "_updObj=null; \n");
		    writer.Write("window.addEvent('domready', function() {\n");
		    //avvio blocco di upload
		    writer.Write("window." + this.ID.Trim() + "_updObj = new UploadObject ( '" + this.ID.Trim() + "_updObj', {  \n" );
		    writer.Write("uploader:'"+uploadHandler+"', \n");
			writer.Write("fileformat: '"+this.FileFormat+"', \n");
			writer.Write("instance  : '" + this.ClientID + "', \n");
			writer.Write("width : "+this.Width+", \n");
			writer.Write("height: "+this.Height+", \n");
			if(this.CssClass.Trim().Length>0)
				writer.Write("cssClass:'"+this.CssClass+"', \n");
			writer.Write("savePath: '"+this.SavePath+"', \n");
			writer.Write("preview : {show:"+this.ShowPreview+", width:"+this.PreviewWidth+",height:"+this.PreviewHeight+"}, \n");
			writer.Write("maxsize:"+this.MaxUploadSize+", \n");
			writer.Write("unique:"+this.UniqueName.ToString().ToLower()+" \n");
			writer.Write("} );\n");
		    //blocco di upload finito.
		    writer.Write("});\n");
		    /*
		    writer.Write(" window."+this.ID.Trim() + "mupdC_UpdateContent =function(url,wrapper) { \n");
		    writer.Write(" var evo = $('" + this.ClientID + "'); \n");
		    writer.Write(" if(evo) { evo.set('value',url);} \n");
		    writer.Write(" } \n");
		    writer.Write("// ]]> \n");
		    */
            writer.Write("</script>\n");
            //mioTestmupdC_UpdateContent
            //testmupdC_UpdateContent is not a function
        }
        
	}
}
